package com.timesheet.gui.efforttracker;

import java.util.Calendar;
import java.util.List;

import com.example.timesheet.uitl.DateUtil;
import com.timesheet.gui.efforttracker.providers.EffortTableLabelProver;
import com.timesheet.gui.forms.FormView;
import com.timesheet.server.common.models.view.EffortTrackerViewModel;
import com.timesheet.serverice.EffortTrackerService;

import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;

public class ViewEffortsWSR extends FormView {

	private TableViewer viewer;

	final String[] columnPropertyNames = { "taskType", "taskNo", "taskDescription", "hours" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

	final String[] columnHeaders = { "TASKTYPE", "TASKNO", "TASKDESCRIPTION", "HOURS" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

	public void createPartControl(Composite parent) {
		super.createPartControl(parent);

		Composite bodyContainer = getForm().getBody();
		//getForm().setText("ss");
		bodyContainer.setLayout(new FillLayout());

		Composite container = getToolkit().createComposite(bodyContainer, SWT.NONE);
		container.setLayout(new GridLayout(1, false));
		{
			Composite upperComposite = getToolkit().createComposite(container, SWT.NONE);
			upperComposite.setLayout(new GridLayout(3, false));
			upperComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

			Label lblChooseWeekdate = getToolkit().createLabel(upperComposite, "Choose WeekDate", SWT.NONE);

			final DateTime dateTime = new DateTime(upperComposite, SWT.BORDER | SWT.DATE | SWT.DROP_DOWN);

			Button btnSubmit = getToolkit().createButton(upperComposite, "Submit", SWT.NONE);
			btnSubmit.addSelectionListener(new SelectionAdapter() {

				@Override
				public void widgetSelected(SelectionEvent e) {
					int day = dateTime.getDay();
					int year = dateTime.getYear();
					int month = dateTime.getMonth();
					Calendar cal = Calendar.getInstance();
					cal.set(Calendar.DAY_OF_MONTH, day);
					cal.set(Calendar.MONTH, month);
					cal.set(Calendar.YEAR, year);

					String dateAsString = DateUtil.getDateAsString(cal.getTime());
					System.out.println("Date: " + dateAsString);

					EffortTrackerService effortTrackerService = new EffortTrackerService();

					/*	List<EffortTrackerViewModel> convertEffortsToViewModelList = effortTrackerService
								.convertEffortsToViewModelList(cal.getTime(),1);*/

					List<EffortTrackerViewModel> convertEffortsToViewModelList = null;

					final IObservableList input =
							new WritableList(convertEffortsToViewModelList, EffortTrackerViewModel.class);
					setInput(convertEffortsToViewModelList);
					viewer.refresh();

				}
			});
			btnSubmit.setText("Submit");
		}
		{
			Composite tableComposite = new Composite(container, SWT.NONE);
			tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
			viewer = new TableViewer(tableComposite, SWT.FULL_SELECTION);

			// createTable(viewer.getTable());
		}

		createTableRidget(viewer.getTable());
	}

	public void setFocus() {

	}

	public void createTable(Table table) {
		createTableRidget(table);

	}

	public void setInput(List<EffortTrackerViewModel> input) {
		viewer.setContentProvider(new ArrayContentProvider());
		viewer.setLabelProvider(new EffortTableLabelProver());
		viewer.setInput(input);
		/*tableRidget.bindToModel(input, EffortTrackerViewModel.class,
				columnPropertyNames, columnHeaders);
		tableRidget.updateFromModel();*/
	}

	private void createTableRidget(Table table) {
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		//tableRidget = (ITableRidget) SwtRidgetFactory.createRidget(table);
	}

	public void setValueTable() {

	}

}
